package com.mlan.userap;

import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.google.android.material.bottomsheet.BottomSheetDialog;
import com.google.android.material.navigation.NavigationBarView;
import com.mlan.userap.activity.BaseActivity;
import com.mlan.userap.fragment.ChatFragment;
import com.mlan.userap.fragment.HomeFragment;
import com.mlan.userap.fragment.MeFragment;
import com.mlan.userap.fragment.VideoFragment;
import com.mlan.userap.model.MsgEvent;
import com.mlan.userap.untils.WebSocketManager;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

public class MainActivity extends BaseActivity {

    private BottomNavigationView bottomNavigationView;
    private TextView tvTitle;
    private FragmentManager fragmentManager;
    private Fragment currentFragment;

    private HomeFragment homeFragment;
    private ChatFragment chatFragment;
    private VideoFragment videoFragment;
    private MeFragment meFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        WebSocketManager.getInstance().connect();

        // 初始化FragmentManager
        fragmentManager = getSupportFragmentManager();

        // 初始化Fragments

        homeFragment = HomeFragment.getInstance();
        chatFragment = ChatFragment.getInstance();
        videoFragment = VideoFragment.getInstance();
        meFragment = MeFragment.getInstance();

        // 初始化底部导航栏
        bottomNavigationView = findViewById(R.id.bottom_navigation);

        // 为通话图标单独设置固定颜色
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            bottomNavigationView.getMenu().findItem(R.id.navigation_call)
                    .setIconTintList(getResources().getColorStateList(R.color.call_icon_color, getTheme()));
        }

        bottomNavigationView.setOnItemSelectedListener(new NavigationBarView.OnItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                return changeMenuId(item.getItemId());
            }
        });

        // 默认加载首页Fragment
        if (savedInstanceState == null) {
            currentFragment = new ChatFragment();
            fragmentManager.beginTransaction()
                    .replace(R.id.fragment_container, currentFragment)
                    .commit();
            bottomNavigationView = findViewById(R.id.bottom_navigation);
            bottomNavigationView.getMenu().findItem(R.id.navigation_home).setChecked(true);
        }

        bottomNavigationView.setItemIconTintList(null); // 移除默认颜色
        for (int i = 0; i < bottomNavigationView.getMenu().size(); i++) {
            MenuItem menuItem = bottomNavigationView.getMenu().getItem(i);
            if (menuItem.getItemId() == R.id.navigation_call) {
                View customCallView = LayoutInflater.from(this).inflate(R.layout.custom_nav_item_call, bottomNavigationView, true);
                menuItem.setActionView(customCallView);
            } else {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                    menuItem.setIconTintList(getResources().getColorStateList(R.color.bottom_navigation_colors, getTheme()));
                }
            }
        }
        // switchFragment(chatFragment);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void msgEvent(MsgEvent event) {
        Log.d("zdl", "====MainActivity===108======" + event.data);
        if (!event.data.toString().equals("ok")){
            startCallActivity(event.data.toString());
        }
    }

    private boolean changeMenuId(int menuId) {
        if (menuId == R.id.navigation_home) {
            switchFragment(chatFragment);
            return true;
        } else if (menuId == R.id.navigation_chat) {
            switchFragment(homeFragment);
            return true;
        } else if (menuId == R.id.navigation_call) {
            showCallDialog();
            return true;
        } else if (menuId == R.id.navigation_video) {
            switchFragment(videoFragment);
            return true;
        } else if (menuId == R.id.navigation_me) {
            switchFragment(meFragment);
            return true;
        }
        return false;
    }

    // 切换Fragment
    private void switchFragment(Fragment targetFragment) {
//        if (fragment != null && fragment != currentFragment) {
//            FragmentTransaction transaction = fragmentManager.beginTransaction();
//            transaction.replace(R.id.fragment_container, fragment);
//            transaction.commit();
//            currentFragment = fragment;
//        }
        FragmentTransaction transaction = fragmentManager.beginTransaction();

        // 隐藏当前Fragment
        if (currentFragment != null) {
            transaction.hide(currentFragment);
        }

        // 显示目标Fragment
        if (!targetFragment.isAdded()) {
            transaction.add(R.id.fragment_container, targetFragment, targetFragment.getClass().getSimpleName());
        } else {
            transaction.show(targetFragment);
        }

        transaction.commit();
        currentFragment = targetFragment;
    }

    // 显示通话选项弹窗
    private void showCallDialog() {
        BottomSheetDialog bottomSheetDialog = new BottomSheetDialog(this);
        View dialogView = getLayoutInflater().inflate(R.layout.layout_call_dialog, null);
        bottomSheetDialog.setContentView(dialogView);

        // 初始化弹窗中的按钮
        ImageView btnVoiceCall = dialogView.findViewById(R.id.btn_voice_call);
        ImageView btnVideoCall = dialogView.findViewById(R.id.btn_video_call);

        // 语音通话按钮点击事件
        btnVoiceCall.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "发起语音通话", Toast.LENGTH_SHORT).show();
                bottomSheetDialog.dismiss();
            }
        });

        // 视频通话按钮点击事件
        btnVideoCall.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "发起视频通话", Toast.LENGTH_SHORT).show();
                bottomSheetDialog.dismiss();
            }
        });

        // 显示弹窗
        bottomSheetDialog.show();
    }
}